package com.googlecode.marrowboy.assertionresult;

import java.util.ArrayList;
import java.util.List;

/**
 * Encapsulates a single test method, (possibly) containing multiple test
 * assertions.
 * 
 * @author mjg
 */
public class MethodDetail {

	private final String name;
	private final List<AssertionDetail> assertionResults;

	/**
	 * Constructor
	 * 
	 * @param methodName
	 *            the name of the method
	 */
	public MethodDetail(String methodName) {
		assertionResults = new ArrayList<AssertionDetail>();
		this.name = methodName;
	}

	/**
	 * Getter
	 * 
	 * @return the name of the method
	 */
	public String getName() {
		return name;
	}

	/**
	 * Add an {@link AssertionDetail} to this method
	 * 
	 * @param assertionResult
	 *            the {@link AssertionDetail} to add
	 */
	public void addAssertion(AssertionDetail assertionResult) {
		assertionResults.add(assertionResult);
	}

	/**
	 * How many assertions passed in this method?
	 * 
	 * @return the number of assertions which passed
	 */
	public int getPassCount() {
		int count = 0;
		for (AssertionDetail assertionResult : assertionResults) {
			if (assertionResult.wasSuccessful()) {
				count++;
			}
		}
		return count;
	}

	/**
	 * How many assertions failed in this method?
	 * 
	 * @return the number of assertions which failed
	 */
	public int getFailCount() {
		int count = 0;
		for (AssertionDetail assertionResult : assertionResults) {
			if (!assertionResult.wasSuccessful()) {
				count++;
			}
		}
		return count;
	}

	/**
	 * How many assertions in this class?
	 * 
	 * @return the number of assertions
	 */
	public int getTotalCount() {
		return assertionResults.size();
	}

	/**
	 * Convenience method to check if all assertions passed
	 * 
	 * @return whether all the assertions passed
	 */
	public boolean allPassed() {
		return (getFailCount() == 0);
	}

	/**
	 * Getter
	 * 
	 * @return A {@link List} of {@link AssertionDetail}s in this method
	 */
	public List<AssertionDetail> getAssertionResults() {
		return assertionResults;
	}

}
